#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Cashflows/CmsCouponPricer.h>
#pragma unmanaged 
#include <ql\cashflows\conundrumpricer.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Cashflows {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IHaganPricer
	public ref class CHaganPricer  : 
            public CCmsCouponPricer,
            public Cephei::QL::Cashflows::IHaganPricer
	{
	protected: 
		boost::shared_ptr<QuantLib::HaganPricer>* _ppHaganPricer;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::HaganPricer>* _phHaganPricer;
#endif
		Object^ _HaganPricerOwner;     // reference to object that manages the storage for this object
	internal:
        CHaganPricer (boost::shared_ptr<QuantLib::HaganPricer>& childNative, Object^ owner);
        CHaganPricer (QuantLib::HaganPricer& childNative, Object^ owner);
        CHaganPricer (CHaganPricer^ copy);
        CHaganPricer (PLATFORM::Type^ t);
#ifdef STRUCT
        CHaganPricer (QuantLib::HaganPricer childNative);
#endif       
#ifdef HANDLE
		CHaganPricer (QuantLib::Handle<QuantLib::HaganPricer>& childNative, Object^ owner);
		CHaganPricer (QuantLib::Handle<QuantLib::HaganPricer> childNative);
#endif
		virtual ~CHaganPricer ();
		!CHaganPricer ();

	internal:
		QuantLib::HaganPricer& GetReference ();
		boost::shared_ptr<QuantLib::HaganPricer>& GetShared ();
		QuantLib::HaganPricer* GetPointer ();
        void SetHaganPricer (boost::shared_ptr<QuantLib::HaganPricer> native)
        {
            if (_ppHaganPricer != NULL)
                delete _ppHaganPricer;
            _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (native);
            SetCmsCouponPricer (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::HaganPricer>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double CapletPrice (Double effectiveCap) ;
		virtual Double CapletRate (Double effectiveCap) ;
		virtual Double FloorletPrice (Double effectiveFloor) ;
		virtual Double FloorletRate (Double effectiveFloor) ;
        property Double MeanReversion 
        {
		    virtual Double get () ;
        }
		virtual Cephei::QL::Cashflows::IHaganPricer^ SetMeanReversion (Cephei::QL::IQuote^ meanReversion) ;
        property Double SwapletPrice 
        {
		    virtual Double get () ;
        }
        property Double SwapletRate 
        {
		    virtual Double get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IHaganPricer Vector interface
	ref class CHaganPricerVector sealed : 
							    public Core::INativeVector<IHaganPricer^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IHaganPricer^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::HaganPricer>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::HaganPricer>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::HaganPricer>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CHaganPricerVector ();
#ifdef STRUCT
		CHaganPricerVector (std::vector<QuantLib::HaganPricer>& vec, Object^ owner);
		CHaganPricerVector (std::vector<QuantLib::HaganPricer> vec);
#endif       
		CHaganPricerVector (std::vector<boost::shared_ptr<QuantLib::HaganPricer>>& vec, Object^ owner);
#ifdef HANDLE
		CHaganPricerVector (std::vector<QuantLib::Handle<QuantLib::HaganPricer>>& vec, Object^ owner);
#endif
		virtual ~CHaganPricerVector ();
		!CHaganPricerVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IHaganPricer^ default[int] 
		{
			virtual IHaganPricer^ get (int index) = Core::INativeVector<IHaganPricer^>::default::get;
			virtual void set (int index, IHaganPricer^ value) = Core::INativeVector<IHaganPricer^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Cashflows::IHaganPricer^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IHaganPricer^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IHaganPricer^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IHaganPricer^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IHaganPricer^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::HaganPricer>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::HaganPricer>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::HaganPricer>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Cashflows::IHaganPricer^>::typeid)]
	[FactoryFor(Cephei::QL::Cashflows::IHaganPricer::typeid)]
	[FactoryFor(Cephei::QL::Cashflows::IHaganPricer_Factory::typeid)]
	public ref class CHaganPricer_Factory sealed : public IHaganPricer_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of IHaganPricer, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IHaganPricer^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IHaganPricer, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IHaganPricer^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IHaganPricer^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IHaganPricer^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IHaganPricer^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IHaganPricer^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Cashflows::IHaganPricer^>^ CreateNativeVector ()
		{
			return REFNEW CHaganPricerVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Cashflows::IHaganPricer^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Cashflows::IHaganPricer^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Cashflows */}
